import mx.utils.Delegate;

/**
 * Debug class that broadcasts MTASC's trace messages to XTrace
 * Based on Michael Biancos original class called "com.mab.util.debug"
 * modified by Jens Krause [www.websector.de]
 * 
 * @link		http://developer.mabwebdesign.com/xtrace.html
 * @author 		Jens Krause [www.websector.de]
 * @date		02/19/07
 *  
 */
 
class com.mab.util.DebugLight 
{
	private var socket: XMLSocket;
	private var msgStack: Array;
	
	private var connected: Boolean = true;
	private var waitForSocketConnection: Boolean = false;
	
	public static var NORMAL: String = "[NORMAL]";
	public static var DEBUG: String = "[DEBUG]";
	public static var WARN: String = "[WARN]";
	public static var CRITICAL: String = "[CRITICAL]";
				
	/**
	* constructor
	*/	
	public function DebugLight () {};
	
	/**
	* traces messages
	* @param	str - a string to send to the debugger text field
	*
	*/
	public function trace(str: String): Void 
	{
		var message: String = str;
		message += "\n"; //add the newline to end of the str
		
		if(socket && connected) 
		{//we have a socket server connection!
			socket.send(message);
			return;
		} 
		else if(socket && waitForSocketConnection) 
		{
			if(!msgStack) 
			{
				msgStack = new Array();
			}
			
			msgStack.push(message);
			return; //make sure this message doesn't do anywhere but the msg stack
		}
	}
	
	/**
	* Initalizes a socket connection to send trace() input to.
	* If the connection succeeds all output is sent to the socket server instead of the textfield
	* @param	h - String specifing a specific host to connect to. If this argument is not specified "localhost" is used
	* @param	p - Number specifing the port to connect to on the host. If this argument is not specified
	* @return	Boolean - Return value from XMLSocket.connect, 
	* 			if this function returns true it actually doesn't mean you've successfully connected with the server 
	*/
	public function initSocket(h: String, p: Number): Boolean 
	{
		socket = new XMLSocket();				
		socket.onConnect = Delegate.create(this, onConnectSocket);				
		socket.onClose = Delegate.create(this, onCloseSocket);
		
		return socket.connect(h ? h : "localhost", p ? p : 9994);
	}
	
	/**
	* Listens for sockets connect callback
	* @param	succes	Socket connection is successfully established (true or false)
	*/	
	private function onConnectSocket (success: Boolean): Void 
	{
		if(success) 	
		{
			trace("Successfull connection to socket server!");
		} 
		else 
		{
			trace("Connection to server failed!");
			this.socket = null; //set the socket to null
		}
		
		this.connected = success;
		
		this._clearMsgStack(); //clear the message stack		
	};
	
	/**
	* Listens for sockets close callback
	*/	
	private function onCloseSocket (): Void 
	{
		trace("Connection lost");
		this.connected = false;		
	};
	
	/**
	* Clears the msgStack if there is one. This is called from the sockets onConnect event, and should not be called anywhere else.
	*/
	private function _clearMsgStack(Void):Void 
	{
		if(socket && connected && waitForSocketConnection && msgStack) 
		{//if we have a message stack and we have a successfull connection to the server
			while(msgStack.length) 
			{
				socket.send(msgStack.shift());
			}
			
			msgStack = null;
		} 
		else if(!socket && !connected && waitForSocketConnection && msgStack) 
		{//if we have a message stack but no successful connection
			while(msgStack.length) 
			{
				trace(String(msgStack.shift()));
			}
			
			msgStack = null;
		}
	}
}